//安装浏览器运行环境：npm -g install jsdom
const jsdom = require("jsdom");
const {JSDOM} = jsdom;
const dom = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`);
window = dom.window;
document = window.document;
//使用给定的 src 加载脚本
function loadScript(src) {
    // 创建一个 <script> 标签，并将其附加到页面
    // 这将使得具有给定 src 的脚本开始加载，并在加载完成后运行

    let script = document.createElement('script');
    script.src = src;
    document.head.append(script);
    console.info("loadScript");
}
// loadScript("/script.js");

// newFunction();//=>ReferenceError: newFunction is not defined

function loadScript(src, callback) {
  let script = document.createElement('script');
  script.src = src;
  script.onload = () => callback(null, script);
  script.onerror = () => callback(new Error(`Script load error for ${src}`));
  document.head.append(script);
}

loadScript('https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.2.0/lodash.js', script => {
  console.info(`酷，脚本 ${script.src} 加载完成`);
  console.info( _ ); // _ 是所加载的脚本中声明的一个函数
});


